package com.eapi.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eapi.eapicommom.model.entity.User;
import com.eapi.project.common.BaseResponse;
import com.eapi.project.common.ErrorCode;
import com.eapi.project.common.ResultUtils;
import com.eapi.project.constant.SystemConstants;
import com.eapi.project.exception.BusinessException;
import com.eapi.project.mapper.CategoryMapper;
import com.eapi.project.model.dto.article.AddCategoryRequest;
import com.eapi.project.model.entity.Article;
import com.eapi.project.model.entity.Category;
import com.eapi.project.model.vo.CategoryVo;
import com.eapi.project.service.ArticleService;
import com.eapi.project.service.CategoryService;
import com.eapi.project.utils.BeanCopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private ArticleService articleService;

    @Override
    public BaseResponse<List<CategoryVo>> getCategoryList() {
        //查询文章表  状态为已发布的文章
        LambdaQueryWrapper<Article> articleWrapper = new LambdaQueryWrapper<>();
        articleWrapper.eq(Article::getStatus, SystemConstants.ARTICLE_STATUS_NORMAL);
        List<Article> articleList = articleService.list(articleWrapper);
        //获取文章的分类id，并且去重
        Set<Long> categoryIds = articleList.stream()
                .map(article -> article.getCategoryId())
                .collect(Collectors.toSet());

        //查询分类表
        List<Category> categories = listByIds(categoryIds);
        categories = categories.stream().
                filter(category -> SystemConstants.STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());
        //封装vo
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(categories, CategoryVo.class);

        return ResultUtils.success(categoryVos);
    }

    @Override
    public BaseResponse<List<CategoryVo>> getAllCategoryList() {
        List<Category> list = list();
        list = list.stream().
                filter(category -> SystemConstants.STATUS_NORMAL.equals(category.getStatus()))
                .collect(Collectors.toList());
        List<CategoryVo> categoryVos = BeanCopyUtils.copyBeanList(list, CategoryVo.class);
        return ResultUtils.success(categoryVos);
    }

    @Override
    public BaseResponse<String> addCategory(AddCategoryRequest addCategoryRequest, User loginUser) {
        if(!loginUser.getUserRole().equals("admin")) throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        Category category = BeanCopyUtils.copyBean(addCategoryRequest, Category.class);
        category.setCreateBy(loginUser.getId());
        category.setUpdateBy(loginUser.getId());
        save(category);
        return ResultUtils.success("添加成功");
    }
}
